package com.nt.train.controller;

import com.nt.train.dto.CodeVo;
import com.nt.train.dto.SysDemo;
import com.nt.train.service.Shape;
import com.nt.train.service.SysDemoService;
import com.nt.train.service.factory.ShapeBeanFactory;
import com.nt.train.service.factory.ShapeFactory;
import com.nt.train.utils.DateUtil;
import com.nt.train.utils.ToolsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
 * http://t.zoukankan.com/east7-p-13412365.html
 * crud测试action
 */
@Controller
@RequestMapping(value = "/${adminPath}/sysDemo")
public class SysDemoAction extends BaseAction {

	@Autowired
	private SysDemoService sysDemoService;

	@Autowired
	private ShapeBeanFactory factory; // 使用注解注入

	@GetMapping("/drawMyShape")
	@ResponseBody
	public CodeVo drawMyShape(){
		shapeFactoryDraw();
		shapeBeanFactoryDraw();
		return sendOk();
	}
	private void shapeFactoryDraw() {
		System.out.println("======= shapeFactory =======");
		//获取 Circle 的对象，并调用它的 draw 方法
		Shape shapeInterface1 = ShapeFactory.getShape("circle");
		//调用 Circle 的 draw 方法
		shapeInterface1.draw();

		Shape shapeInterface2 = ShapeFactory.getShape("rectangle");
		shapeInterface2.draw();
	}

	private void shapeBeanFactoryDraw() {
		System.out.println("======= 实现二 shapeBeanFactory =======");
		Shape shapeInterface1 = factory.getShape("circle");
		shapeInterface1.draw();

		Shape shapeInterface2 = factory.getShape("square");
		shapeInterface2.draw();
	}



	@RequestMapping(value = "")
	public String index() {
		return "sys/demo/sys_demo";
	}

	/**
	 * 添加
	 */
	@RequestMapping(value = "/saveSysDemo", method = RequestMethod.POST)
	@ResponseBody
	public Object saveSysDemo(HttpServletRequest request, @RequestBody  SysDemo sysDemo) {
		sysDemo.setId(ToolsUtil.getUUID());
		sysDemo.setCreateTime(DateUtil.getNow());
		sysDemoService.save(sysDemo);
		return sendOk();
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/updateSysDemo", method = RequestMethod.POST)
	//@ModelAttribute
	@ResponseBody
	public Object updateSysDemo(HttpServletRequest request,@RequestBody SysDemo sysDemo) {
		sysDemoService.update(sysDemo);
		return sendOk();
	}

	/**
	 * 删除
	 * @param ids id字符串多个使用,分隔
	 */
	@RequestMapping(value = "/deleteSysDemo", method = RequestMethod.POST)
	@ResponseBody
	public Object deleteSysDemo(HttpServletRequest request, @RequestParam String ids) {
		sysDemoService.delete(ids.split(","));
		return sendOk();
	}

	/**
	 * 分页查询
	 * @param request 	HttpServletRequest对象
	 * @param page 当前页码
	 * @param rows 每页条数
	 * @param params 查询参数
	 * @return 返回查询结果json
	 */
	@RequestMapping(value = "/findSysDemo", method = RequestMethod.POST)
	@ResponseBody
	public Object findSysDemo(HttpServletRequest request, int page, int rows, Map<String, Object> params) {
		sysDemoService.queryPage(page, rows, params);
		return sendOk();
	}
}